package com.stone.widget;

import com.stone.R;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.text.format.Time;
import android.widget.RemoteViews;

public class ESTTime extends AppWidgetProvider {
   @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
		int[] appWidgetIds) {
	// TODO Auto-generated method stub
	super.onUpdate(context, appWidgetManager, appWidgetIds);
     context.startService(new Intent(context,UpdateService.class));
   }
   //因为BroadcastReceivce生命周期太短，所以将其放入到service中处理。
    public static class UpdateService extends Service{
        @Override
        public void onStart(Intent intent, int startId) {
        	// TODO Auto-generated method stub
        	super.onStart(intent, startId);
        	Time estTime=new Time("EST");
        	estTime.setToNow();
        	//根据包名和布局文件构建RemoteViews对象
        	RemoteViews updateViews=new RemoteViews(getPackageName(),R.layout.digitalclock);
        	//设定控制内容
        	updateViews.setTextViewText(R.id.time, estTime.format("%H:%M"));
        	//获取组建的名字
        	ComponentName thisWidget=new ComponentName(this,ESTTime.class);
        	//获取AppWidgetManager
        	AppWidgetManager manager=AppWidgetManager.getInstance(this);
        	//进行更新。
        	manager.updateAppWidget(thisWidget, updateViews);
        }
		@Override
		public IBinder onBind(Intent intent) {
			// TODO Auto-generated method stub
			return null;
		}
       	
    }
}
